package com.study.controller;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: MI
 * @Date: 2021/9/14/21:19
 * @Description:
 */
@Api(tags = "ztree-测试")
@Slf4j
@Controller
@RequestMapping("zt")
public class ZtreeController {

    ArrayList<ZtreeVo> ztreeVos = new ArrayList<>();

    @GetMapping
    public String index() {
        System.out.println(LocalDateTime.now());
        return "ztree";
    }

    @PostConstruct
    public void init() {

        ztreeVos.add(new ZtreeVo(1, 0, "父节点1", true));
        ztreeVos.add(new ZtreeVo(11, 1, "父节点11", false));
        ztreeVos.add(new ZtreeVo(111, 11, "叶子节点111", false));
        ztreeVos.add(new ZtreeVo(114, 11, "叶子节点114", false));
        ztreeVos.add(new ZtreeVo(12, 1, "父节点2", true));
        ztreeVos.add(new ZtreeVo(121, 12, "叶子节点121", true));
    }

    @ResponseBody
    @GetMapping("ztree")
    public List<ZtreeVo> ztree() {
        return ztreeVos;
    }

    @ResponseBody
    @GetMapping("query/{id}")
    public Map query(@PathVariable("id") String id) {
        System.out.println("查询的是：" + id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", ztreeVos.size() + "");
        map.put("rows", ztreeVos);
        return map;
    }


   /*  {id: 1, pId: 0, name: "父节点1 - 展开", open: true},
            {id: 11, pId: 1, name: "父节点11 - 折叠"},
            {id: 111, pId: 11, name: "叶子节点111"},
            {id: 114, pId: 11, name: "叶子节点114"},
            {id: 12, pId: 1, name: "父节点12 - 折叠"},
            {id: 121, pId: 12, name: "叶子节点121"}*/

    @Setter
    @Getter
    @NoArgsConstructor
    @AllArgsConstructor
    private static class ZtreeVo {
        private int id;
        private int pId;
        private String name;
        private boolean open;
    }


}
